<?php

namespace app\api\controller;

use app\admin\model\District;
use app\common\controller\Api;
use think\Request;

/**
 * 银行卡接口
 */
class Banks extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new \app\admin\model\user\Banks();
    }

    /**
     * 获取银行列表
     * @ApiMethod   (GET)
     *
     */
    public function index()
    {
        // 1是个人，2是对公
        $type = $this->request->post('type', 1);
        $this->request->filter(['strip_tags']);
        $list = $this->model
            ->where('user_id', $this->auth->id)
            ->where('type', $type)
            ->order('id desc')
            ->select();

        $this->success('银行卡列表为', $list);
    }

    /**
     * 添加银行卡
     * @throws \think\Exception
     */
    public function create()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $data = $this->request->post();
        if ($data['status'] == 1) {
            $this->model->where('type', $data['type'])->where('user_id', $data['user_id'])->setField('status', 0);
        }

        if (!$data['mobile'] || !$data['bankname'] || !$data['bankholder'] || !$data['banknumber']) {
            $this->error('手机号码，银行名称，银行卡号，持有人缺一不可！');
        }

        $number = $this->model->where(['user_id' => $data['user_id']])->count();
        if ($number == 0) {
            $data['status'] = 1;
        }
        $bdata = [
            'user_id' => $this->auth->id,
            'type' => $data['type'],
            'mobile' => $data['mobile'],
            'bankname' => $data['bankname'],
            'bankholder' => $data['bankholder'],
            'banknumber' => $data['banknumber'],
            'status' => $data['status'],
        ];

        $res = $this->model->create($bdata);
        if (!$res->id) {
            $this->error('添加失败！');
        }
        $this->success('添加成功');
    }

    /**
     * 编辑银行卡
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $data = $this->request->post();

        $id = $data['id'];
        $row = $this->model->find($id);
        if (!$row) {
            $this->error('该银行卡不存在！');
        }

        unset($data['token']);
        $data['user_id'] = $this->auth->id;

        $count = $this->model->where(['user_id' => $data['user_id'], 'type' => $row->type, 'id' => ['<>', $id]])->count();
        if ($count == 0) {
            $data['status'] = 1;
            $this->model->allowField(true)->save($data, ['id' => $data['id']]);
        } else {
            if ($data['status'] == 1) {
                $this->model->where('user_id', $data['user_id'])->where('type', $row->type)->setField('status', 0);
            }
            $this->model->allowField(true)->save($data, ['id' => $data['id']]);
        }

        $this->success('编辑成功');
    }

    /**
     * 银行卡详情
     */
    public function read()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $userId = $this->auth->id;
        $row = $this->model->where('id', $id)->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('该银行卡不存在!');
        }

        $this->success('银行卡详情为', $row);
    }

    /**
     * 删除银行卡
     */
    public function delete()
    {
        if (!$this->request->isPost()) {
            $this->error('非法操作');
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $userId = $this->auth->id;
        $row = $this->model->where('id', $id)->where('user_id', $userId)->find();
        if (!$row) {
            $this->error('该银行卡不存在!');
        }

        $res = $this->model->where('id', $id)->delete();
        if (!$res) {
            $this->error('删除失败！');
        }

        $this->success('删除成功！');
    }
}
